<template>
  <div>
    <el-row>
      <el-col>
        <el-form :inline="true" :model="resourceFilter" @keyup.enter.native="getDataList()" size="small">
          <el-form-item label="服务名称">
            <el-input v-model="resourceFilter.resourceName" style="width: 215px" placeholder="服务名称"></el-input>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
                v-model="resourceFilter.useTimeRange"
                type="daterange"
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="success" @click="getDataList()">查询</el-button>
            <el-button type="primary" @click="createService">创建服务</el-button>
          </el-form-item>
        </el-form>
        <el-table
            :data="dataList"
            border
            v-loading="dataListLoading"
            @selection-change="selectionChangeHandle"
            style="width: 100%;">
          <el-table-column
              type="selection"
              header-align="center"
              align="center"
              width="50">
          </el-table-column>
          <el-table-column
              prop="resourceName"
              header-align="center"
              align="center"
              label="服务名称">
          </el-table-column>
          <el-table-column
              prop="resourceDescribe"
              header-align="center"
              align="center"
              label="服务描述">
          </el-table-column>
          <el-table-column
              prop="isStartup"
              header-align="center"
              align="center"
              label="开启/关闭">
            <template #default="scope">
              <el-switch
                  @change="updatedState(scope.row)"
                  v-model="scope.row.isStartup"
                  active-value="0"
                  inactive-value="1">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column
              prop="visitRestriction"
              header-align="center"
              align="center"
              label="开放限制"
              :formatter="showServLimit">
          </el-table-column>
          <el-table-column
              prop="servKeyword"
              header-align="center"
              align="center"
              label="关键词">
          </el-table-column>
          <el-table-column
              prop="createdTime"
              header-align="center"
              align="center"
              label="发布时间">
          </el-table-column>
          <el-table-column
              fixed="right"
              header-align="center"
              align="center"
              width="150"
              label="操作">
            <template #default="scope">
              <!--            <el-button type="primary" size="mini" @click="updateService(scope.row)">编辑</el-button>-->
              <el-button type="primary" size="mini" @click="deleteService(scope.row.servId)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="page.currentPage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="page.pageSize"
            :total="page.total"
            layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </el-col>
    </el-row>
    <!-- 弹窗, 新增 / 修改 -->
    <!--  <service-updated v-if="serviceUpdatedVisible" ref="serviceUpdated" @refreshDataList="getDataList"></service-updated>-->
    <dara-base-interface-edit v-if="serviceUpdatedVisible" ref="serviceUpdated" @refreshDataList="getDataList"></dara-base-interface-edit>
  </div>
</template>

<script>
import pickerShortcut from "@/mixins/picker-shortcut";
import {queryResourceItemPage, updatedResourceItemState} from "@/api/resources-center/service";
import DaraBaseInterfaceEdit from "@/views/data-center/database-interface/DaraBaseInterfaceEdit";


/**
 *{ 文件管理列表 }
 * @param:
 * @author: 宋康
 * @DateTime: 2022/7/8
 */
export default {
  name: "DaraBaseInterfaceList",
  components: {DaraBaseInterfaceEdit},
  mixins: [pickerShortcut],
  data() {
    return {
      resourceFilter: {
        resourceName: '',
        resourceType: '4',
        useTimeRange: []
      },
      dataList: [],
      page: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      dataListLoading: false,
      dataListSelections: [],
      serviceUpdatedVisible: false
    };
  },
  created () {
    this.getDataList()
  },
  methods: {
    /**
     * {获取资源列表}
     *
     * @author: 宋康
     * @date: 2023/2/3
     */
    getDataList() {

      this._debounce(() => queryResourceItemPage(this.page.currentPage, this.page.pageSize, this.resourceFilter).then(res => {
        if (res.status) {
          const data = res.data;
          this.dataList = data.records;
          this.page.total = data.total;
        }
      }), 500);
    },
    // 每页数
    sizeChangeHandle (val) {
      this.page.pageSize = val
      this.page.currentPage = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.page.currentPage = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },

    updatedState(data){
      updatedResourceItemState(data.resourceId, data.isStartup).then(res=>{
        if (res.status) {
          this._success("修改完成")
          this.getDataList();
        } else {
          this._error(res.msg)
        }
        // 刷新页面
        this.$router.go(0)
      })
    },

    /**
     * 创建服务
     */
    createService() {
      this.$router.push({
        name: "DaraBaseInterfaceAdd",
      });
    },
    /**
     * 修改服务
     */
    updateService(row) {
      this.serviceUpdatedVisible = true
      this.$nextTick(() => {
        this.$refs.serviceUpdated.init(row.resourceId)
      })
    },
    /**
     *{ 删除服务 }
     * @param:
     * @author: mk
     * @DateTime: 2022/5/3
     */
    deleteService(servId) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDataBase(servId).then(res=>{
          if(res.status){
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            // 刷新页面
            this.$router.go(0)
          }else {
            this.$message.error('删除失败');
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    showServLimit(row, column, cellValue, index){
      if(cellValue==1){
        return '可见'
      }
      if(cellValue==2){
        return '条件可见'
      }
      if(cellValue==3){
        return '线下'
      }
    }
  }
};
</script>

<style lang="scss" scoped>

</style>
